package com.nidu.demo.company.ability;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.company.gateway.CompanyGateway;
import com.nidu.demo.company.model.Company;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class CompanyDomainService {

    private final CompanyGateway gateway;

    public void validateDuplicate(String name, Long excludeId){
        Company company = gateway.getByName(name);
        if(company != null){
            if(excludeId == null){
                throw new BizException(ErrorCodeConstants.COMPANY_NAME_DUPLICATE.getCode(),
                        ErrorCodeConstants.COMPANY_NAME_DUPLICATE.getMessage());
            } else if (!excludeId.equals(company.getId())) {
                throw new BizException(ErrorCodeConstants.COMPANY_NAME_DUPLICATE.getCode(),
                        ErrorCodeConstants.COMPANY_NAME_DUPLICATE.getMessage());
            }
        }
    }

    public Company validateCompany(Long id) {
        Company company = gateway.getById(id);
        if (company == null) {
            throw new BizException(ErrorCodeConstants.COMPANY_NOT_FOUND.getCode(),
                    ErrorCodeConstants.COMPANY_NOT_FOUND.getMessage());
        }
        company.isDisable();
        company.isDeleted();
        return company;
    }
}
